import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/Layout/Layout.vue'
import Home from '@/views/Home/Home.vue'
import useStore from '@/store'
import Message from '@/components/XtxMessage'

const router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior: () => {
    return {
      top: 0,
    }
  },
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          component: Home
        },
        {
          path: 'category/:id',
          component: () => import('@/views/Category/Category.vue')
        },
        {
          path: 'goods/:id',
          component: () => import('@/views/Goods/Goods.vue')
        },
        {
          path: 'cart',
          component: () => import('@/views/Cart/Cart.vue')
        },
        {
          path: 'member/checkout',
          component: () => import('@/views/member/pay/checkout.vue')
        },
        {
          path: 'member/pay',
          component: () => import('@/views/member/Pay.vue')
        },
        {
          path: 'pay/callback',
          component: () => import('@/views/member/pay/callback.vue')
        }
      ]
    },
    {
      path: '/login',
      component: () => import('@/views/Login/Login.vue')
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/Callback.vue')
    },
    {
      path: '/login/aaa',
      component: () => import('@/views/Login/aaa.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  const { cart } = useStore()
  if (cart.isLogin === false && to.path === '/member/checkout') {
    Message.warning('请登录~')
    const url = 'login?redirect=' + to.fullPath
    next(url)
  } else {
    next()
  }
})
export default router
